package com.huazi.www.yang.medicine.dao.Impl;

import com.huazi.www.yang.medicine.dao.MedicneStockDao;
import com.huazi.www.yang.medicine.entity.DrugStock;
import com.huazi.www.yang.medicine.entity.Medicne;
import com.huazi.www.yang.medicine.uilt.BaseDao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MedicneStockDaoImpl extends BaseDao implements MedicneStockDao {

    ResultSet rs = null;

    /**
     * 分页查询买药申请表
     *
     * @param applycat  申请人模糊查询
     * @param applydrug 药名模糊查询
     * @param start     每页第一条数据的下标
     * @param pageSize  每页显示行数
     * @return
     */
    @Override
    public List<Medicne> selectStock(String applycat, String applydrug, Integer applystatus, Integer start, Integer pageSize) {
        String sql = "select drug_dosing.id,drug_dosing.applycat,drug_dosing.applytime,drug_dosing.drugspecies,drug_dosing.applydrug,drug_remedytype.drugtype,drug_dosing.price,drug_dosing.count,drug_dosing.type,drug_dosing.applystatus,drug_dosing.remark FROM drug_dosing  left join drug_remedytype on drug_dosing.drugtype=drug_remedytype.id WHERE drug_dosing.applystatus=?";
        if (applycat != null && !applycat.equals("") && applydrug != null && !applydrug.equals("")) {
            sql += " and  drug_dosing.applycat LIKE CONCAT('%',?,'%') and  drug_dosing.applydrug LIKE CONCAT('%',?,'%') LIMIT ?,?";
            rs = executeselect(sql, applystatus, start, applycat, applydrug, start, pageSize);
        } else if (applycat != null && !applycat.equals("")) {
            sql += " and  drug_dosing.applycat LIKE CONCAT('%',?,'%') LIMIT ?,?";
            rs = executeselect(sql, applystatus, applycat, start, pageSize);
        } else if (applydrug != null && !applydrug.equals("")) {
            sql += " and  drug_dosing.applydrug LIKE CONCAT('%',?,'%') LIMIT ?,?";
            rs = executeselect(sql, applystatus, applydrug, start, pageSize);
        } else {
            sql += " LIMIT ?,?";
            rs = executeselect(sql, applystatus, start, pageSize);
        }
        List<Medicne> list = new ArrayList<>();
        try {
            String status = "";
            while (rs.next()) {
                Integer id = rs.getInt(1);
                String name = rs.getString(2);
                String time = rs.getString(3);
                Integer drugspecties = rs.getInt(4);
                String yaoName = rs.getString(5);
                String drugtype = rs.getString(6);
                double price = rs.getDouble(7);
                Integer count = rs.getInt(8);
                String type = rs.getString(9);
                Integer applystatu = rs.getInt(10);
                if (applystatu == 0) {
                    status = "未处理";
                } else {
                    status = "已处理";
                }
                String remark = rs.getString(11);
                Medicne medicne = new Medicne(id, name, time, drugspecties, yaoName, drugtype, price, count, type, status, remark);
                list.add(medicne);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return list;
    }

    /**
     * 查询总记录条数
     *
     * @param applycat
     * @param applydrug
     * @return
     */
    @Override
    public int totalCount(String applycat, String applydrug, Integer applystatus) {
        String sql = "SELECT count(1) FROM `drug_dosing` where 1=1 and drug_dosing.applystatus=?";
        if (applycat != null && !applycat.equals("") && applydrug != null && !applydrug.equals("")) {
            sql += " and  drug_dosing.applycat LIKE CONCAT('%',?,'%') and  drug_dosing.applydrug LIKE CONCAT('%',?,'%') LIMIT ?,?";
            rs = executeselect(sql, applystatus, applycat, applydrug);
        } else if (applycat != null && !applycat.equals("")) {
            sql += " and  drug_dosing.applycat LIKE CONCAT('%',?,'%')";
            rs = executeselect(sql, applystatus, applycat);
        } else if (applydrug != null && !applydrug.equals("")) {
            sql += " and  drug_dosing.applydrug LIKE CONCAT('%',?,'%')";
            rs = executeselect(sql, applystatus, applydrug);
        } else {
            rs = executeselect(sql, applystatus);
        }
        int count = 0;
        try {
            while (rs.next()) {
                count = rs.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return count;
    }

    /**
     * 修改 申请同意后修改状态
     *
     * @param id
     */
    @Override
    public void updateStatus(Integer id) {
        String sql = "update drug_dosing SET drug_dosing.applystatus=1 where drug_dosing.id=?";
        executeupdate(sql, id);
    }

    /**
     * 删除
     *
     * @param id
     */
    @Override
    public void delete(Integer id) {
        String sql = "delete from drug_dosing where drug_dosing.id=?";
        executeupdate(sql, id);
    }

    /**
     * 查询申请药的详情药单
     *
     * @return
     */
    @Override
    public List<DrugStock> selectDetails(String date) {
        List<DrugStock> list = new ArrayList<>();
        String sql = "select a.drugid,b.drugname,b.price,a.count from drug_zz as a INNER JOIN drug_remedy as b on a.drugid = b.id where a.date = ?";
        rs = executeselect(sql, date);
        try {
            while (rs.next()) {
                int drugId = rs.getInt(1);
                String drugName = rs.getString(2);
                double price = rs.getDouble(3);
                int count = rs.getInt(4);
                DrugStock drugStock = new DrugStock(drugId, drugName, price, count);
                list.add(drugStock);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return list;
    }
}
